package com.oocl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oocl.entity.Foods;
import com.oocl.service.FoodsService;
import com.oocl.vo.FoodsVo;


@Controller
public class FoodsController {
	@Autowired
   FoodsService foodsService;
	
	
	@ResponseBody
	@RequestMapping(value="/foodslist")
	public List<FoodsVo>  loadListByMid(HttpSession session){
		List<FoodsVo> list = new ArrayList<FoodsVo>();
		list = foodsService.findFoodsByIcnum((String)session.getAttribute("icnum"));
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/updatefood")
	public List<FoodsVo> updateFood(@RequestParam(value="foodFile",required=false) MultipartFile foodFile,
			                  Foods food,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("foodFile", foodFile);
		map.put("food", food);
		System.out.println("foodType:........."+food.getFtype());
		FoodsVo fvo = null;
		try {
			fvo = foodsService.updateFood(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<FoodsVo> list = new ArrayList<FoodsVo>();
		list = foodsService.findFoodsByIcnum((String)session.getAttribute("icnum"));
		return list;
	} 
	
	
	@ResponseBody
	@RequestMapping("/addfood")
	public FoodsVo addFood(@RequestParam(value="foodFile",required=false) MultipartFile foodFile,
			                  Foods food,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		System.out.println(food.getFtype());
		String mid  = (String) session.getAttribute("mid");
		map.put("foodFile", foodFile);
		map.put("food", food);
		map.put("mid", mid);
		FoodsVo fvo =null;
		try {
			fvo = foodsService.addFood(map);
		} catch (Exception e) {
			e.printStackTrace();
			return new FoodsVo();
		}
		return fvo;
	} 
	
	
	@ResponseBody
	@RequestMapping(value="/deletefood")
	public FoodsVo deleteFood( String fid){
		System.out.println(fid);
		FoodsVo fvo = foodsService.deleteFood(fid);
		return fvo;
	} 
	
}
